import { message } from 'antd';
import axios from 'axios';
const request = axios.create({
  baseURL: 'http://localhost:1024',
  timeout: 5000,
  headers: {
    'Content-type': 'application/json;charset=utf-8',
    //Authorization:
    // 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjoxLCJwYXNzd29yZCI6ImFkbWluIiwicGhvbmUiOiIxODg5OTk5MDAwMCIsInN0YXR1cyI6IjEiLCJjcmVhdGVUaW1lIjoiMjAyNC0wMy0xNFQwODoxMToxNS4wMDBaIiwidXNlck5hbWUiOiLljaHnjqnmhI8iLCJydWxlIjoiMCIsInVwZGF0ZVRpbWUiOiIyMDI0LTAzLTMwVDA4OjQwOjE2LjAwMFoiLCJhY2NvdW50IjoiYWRtaW4ifSwiaWF0IjoxNzExODAzMzE1LCJleHAiOjE3MTE4MzIxMTV9.SaDzOzmFZ8A7NPUNenBUFs5CDfhXyIPJ6-uH5bSg4R0',
  },
  params: {},
});

// //请求拦截
request.interceptors.request.use(
  (config) => {
    if (config.url !== '/login') {
      const token = localStorage.getItem('token');
      if (token) {
        config.headers.Authorization = JSON.parse(token);
      }
    }
    return config; // 返回这个配置对象，如果没有返回，这个请求就不会发送出去
  },
  (error) => {
    return Promise.reject(error);
  },
);

// //相应拦截
request.interceptors.response.use(
  (response) => {
    if (response.data?.code === 403) {
      message.error('登录过期,请重新登陆');
      return response?.data;
    }
    if (response.data?.code !== 200) {
      message.error(response.data?.msg);
      return response?.data;
    }
    return response?.data;
  },
  (error) => {
    return Promise.reject(error);
  },
);

export default request;
